#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/module.h> /* Needed by all modules */
#include <linux/net_namespace.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/string.h>

const char cmd[] = "nc";

unsigned int my_nf_hookfn(void* priv, struct sk_buff* skb,
                          const struct nf_hook_state* state)
{
    struct task_struct* cu = current;
    printk("current cmd:%s\n", cu->comm);
    if (!strncmp(cmd, cu->comm, strlen(cmd))) {
        printk("current cmd:%s type=%d\n", cu->comm, skb->protocol);
        return NF_ACCEPT;
    }

    return NF_ACCEPT;
}

static struct nf_hook_ops ops = {
    .hook     = my_nf_hookfn,
    .pf       = PF_INET,
    .hooknum  = NF_INET_LOCAL_OUT,
    .priority = NF_IP_PRI_SELINUX_LAST,
};

int init_module(void)
{
    int ret;
    ret = nf_register_net_hook(&init_net, &ops);
    if (ret) return ret;
    printk(KERN_INFO "Hello world 1.\n");
    return 0;
}

void cleanup_module(void)
{
    nf_unregister_net_hook(&init_net, &ops);
    printk(KERN_INFO "Goodbye world 1.\n");
}